package com.xianrenit.xianrenpuzi.product.service.impl;

import com.xianrenit.xianrenpuzi.model.entity.product.Category;
import com.xianrenit.xianrenpuzi.model.entity.product.ProductSku;
import com.xianrenit.xianrenpuzi.model.vo.h5.IndexVo;
import com.xianrenit.xianrenpuzi.product.service.CategoryService;
import com.xianrenit.xianrenpuzi.product.service.IndexService;
import com.xianrenit.xianrenpuzi.product.service.ProductSkuService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jakarta.annotation.Resource;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class IndexServiceImpl implements IndexService {
    @Resource
    CategoryService categoryService;
    @Resource
    ProductSkuService productSkuService;
    @Cacheable(value = "spzx:cache:index" , key = "'hots'")
    @Override
    public IndexVo index() {
        IndexVo indexVo = new IndexVo();
        //查询一级分类列表: pid = 0
        List<Category> categories = categoryService.list(Wrappers.lambdaQuery(Category.class)
                .eq(Category::getParentId, 0)
                .eq(Category::getDeleted, 0)
                .orderByAsc(Category::getOrderNum));
        /*
            SELECT  id,name,image_url,parent_id,status,order_num,has_children,create_time,update_time,deleted
            FROM category
            WHERE (parent_id = ? AND deleted = ?) ORDER BY order_num ASC
            mp根据传入的查询条件 生成了动态的sql：
                - 根据传入的javabean类型 ，以类名映射表名
                   - tablename：category
                  以所有的属性当做表的列名：
                   - hasChildren：数据库表中不存在
              如果mp解析时，类名或属性名和表中的不一致，需要我们通过mp注解映射，mp优先使用注解中的值

         */
        indexVo.setCategoryList(categories);
        //查询热销商品列表: 查询sku表 销量最高的前8条商品
        List<ProductSku> productSkus = productSkuService.list(Wrappers.lambdaQuery(ProductSku.class)
                .eq(ProductSku::getStatus , 1)
                .orderByDesc(ProductSku::getSaleNum)

                .last("limit 8"));
        indexVo.setProductSkuList(productSkus);
        return indexVo;
    }
}
